//		ID : Xerxes
//      "I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,ed) for(int i = 0 ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define tp pair< int, pair<int,int> >
#define mmk( x, y , z ) mk( x, mk( y, z ) )

inline void wait( double seconds ){
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ){
        ;
    }
}
int R,C;
int G[1000][1000];
int F[1000][1000];
int D[1000][1000];
int r[]={+0,+0,-1,+1};
int c[]={-1,+1,+0,+0};
#define pi pair<int, pair<int,int> >
inline void Read(){
	CI(R);
	CI(C);
	fo(i,R){
		fo(j,C){
			CI(G[i][j]);
		}
	}
}
#define inf 1023456789
inline void ini(){
	SET(F,0);;
	fo(i,1000){
		fo(j,1000){
			D[i][j] = inf;
		}
	}
}
inline int dj(){
	ini();
	set< pi , less< pi >  > pq;
	pq.insert( mk(G[0][0], mk(0,0) ) );
	D[0][0]=G[0][0];
	
	while( pq.empty()==false ){
		int x = pq.begin()->s.f;
		int y = pq.begin()->s.s;
		pq.erase(pq.begin());
		
		if( F[x][y] )continue;
		fo(i,4){
			int tr = x+r[i];
			int tc = y+c[i];
			if( tr<0 || tc < 0 || tr >=R || tc >=C )continue;
			if( F[tr][tc]==1 )continue;
			int nwCost = D[x][y]+ G[tr][tc];
			if( nwCost < D[tr][tc] ){
				D[tr][tc]= nwCost;
				pq.insert( mk(D[tr][tc], mk(tr,tc) ) );
			}
		}
		F[x][y]=1;
	}
	return D[R-1][C-1];
}
inline void Proc(){
	int var = dj();
	cout<<var<<"\n";
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in","rt",stdin);
    #endif
    int cases=1;
    for( CI(cases) ; cases ; --cases ){
			Read();
			Proc();
    }

return 0;
}


